<?php
/*
 +----------------------------------------------------------------------
 + Title        : Api 控制器
 + Author       : Randy_chen
 + Version      : V1.0.0.1
 + Initial-Time : 2023/5/20 22:11
 + Last-time    : 2023/5/20 22:11+ chen2
 + Desc         : Api
 +----------------------------------------------------------------------
*/


namespace app\command\gen;

use randy\tools\units\ExtendArray;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

class Api extends Command
{
    protected function configure()
    {
        $this->setName('gen:api')
             ->setDescription(' 自动生成接口 ');
    }

    protected function execute(Input $input, Output $output)
    {
        $data = file_get_contents('api.json');
        $data = json_decode($data, 1);
        $apis = $data['apis'];
        $apis = ExtendArray::treeToList($apis, 'children', 'mock_url');
        $list = [];
        foreach ($apis as $api) {
            if ($api['target_type'] == 'api') {
                if(!$api['request']['url']){
                    continue;
                }
                $tmp['api_url'] = $api['request']['url'] ?? '';
                $tmp['api_name'] = $api['name'] ?? '';
                $tmp['api_id'] = md5($tmp['api_url']) ?? '';
                $list[] = $tmp;
            }
        }

        \app\admin\model\Api::build()->extra('IGNORE')
            ->insertAll($list);

        echo '接口生成成功!';
    }
}